package com.health.mapp.utils;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;  
import java.awt.Image;  
import java.awt.Transparency;
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;
import java.io.IOException;  
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;  

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
  
/** 
 * * @author WQ * @date 2011-01-14 * @versions 1.0 ͼƬѹ�������� �ṩ�ķ����п����趨��ɵ� 
 * ����ͼƬ�Ĵ�С�ߴ�� 
 */  
public class ImageUtil {  
  
	
	/** * ͼƬ�ļ���ȡ * * @param srcImgPath * @return */  
    private static BufferedImage InputImage(String srcImgPath) {  
        BufferedImage srcImage = null;  
        try {  
            FileInputStream in = new FileInputStream(srcImgPath);  
            srcImage = javax.imageio.ImageIO.read(in);  
        } catch (IOException e) {  
            System.out.println("��ȡͼƬ�ļ����?" + e.getMessage());  
            e.printStackTrace();  
        }  
        return srcImage;  
    }  
  
    /** 
     * 
     *  ��ͼƬ����ָ����ͼƬ�ߴ�ѹ�� 
     *   @param srcImgPath :ԴͼƬ·��
     *   @param outImgPath :�����ѹ��ͼƬ��·�� 
     *    @param new_w  :ѹ�����ͼƬ�� 
     *     @param new_h  :ѹ�����ͼƬ�� 
     */  
    public static void compressImage(String srcImgPath, String outImgPath,  
            int new_w, int new_h) {  
        BufferedImage src = InputImage(srcImgPath);  
        disposeImage(src, outImgPath, new_w, new_h);  
    }  
  
    /** 
     * ָ�������߿�����ֵ��ѹ��ͼƬ 
     *  
     *  @param srcImgPath   :ԴͼƬ·�� 
     *  @param outImgPath   :�����ѹ��ͼƬ��·�� 
     *   @param maxLength   :�����߿�����ֵ 
     */  
    public static void compressImage(String srcImgPath, String outImgPath,  
            int maxLength) {  
        // �õ�ͼƬ  
        BufferedImage src = InputImage(srcImgPath);  
        if (null != src) {  
            int old_w = src.getWidth();  
            // �õ�Դͼ��  
            int old_h = src.getHeight();  
            // �õ�Դͼ��  
            int new_w = 0;  
            // ��ͼ�Ŀ�  
            int new_h = 0;  
            // ��ͼ�ĳ�  
            // ���ͼƬ�ߴ�ѹ���ȵõ���ͼ�ĳߴ�  
            if (old_w > old_h) {  
                // ͼƬҪ���ŵı���  
                new_w = maxLength;  
                new_h = (int)  Math.round(old_h * ((float) maxLength / old_w));  
            } else {  
                new_w = (int)  Math.round(old_w * ((float) maxLength / old_h));  
                new_h = maxLength;  
            }  
            disposeImage(src, outImgPath, new_w, new_h);  
        }  
    }  
  
    /** *
     *  ����ͼƬ
     *   
     *  @param src
     *  @param outImgPath 
     *  @param new_w 
     *  @param new_h 
     *   
     * 
     * */  
    private synchronized static void disposeImage(BufferedImage src,  
            String outImgPath, int new_w, int new_h) {  
        // �õ�ͼƬ  
        int old_w = src.getWidth();  
        // �õ�Դͼ��  
        int old_h = src.getHeight();  
        // �õ�Դͼ��  
        BufferedImage newImg = null;  
        // �ж�����ͼƬ������  
        System.out.print("src.getType():"+src.getType());
        switch (src.getType()) 
        {  
        case 13:  
             // png,gifnewImg = new BufferedImage(new_w, new_h,         BufferedImage.TYPE_4BYTE_ABGR);  
            break;  
        default:  
            newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);  
            break;  
        }  
        Graphics2D g = newImg.createGraphics();  
        
        g.getDeviceConfiguration().createCompatibleImage(new_w,new_h,Transparency.TRANSLUCENT);
        // ��ԭͼ��ȡ��ɫ������ͼ  
        g.drawImage(src, 0, 0, old_w, old_h, null);  
        g.dispose();  
        // ���ͼƬ�ߴ�ѹ���ȵõ���ͼ�ĳߴ�  
        newImg.getGraphics().drawImage(src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0,    null);  
       
        
          

		 

	 
 
        
        
        
        // ���÷������ͼƬ�ļ�  
        OutImage(outImgPath, newImg);  
    }  
  
    /** 
     * * ��ͼƬ�ļ������ָ����·���������趨ѹ������ * * @param outImgPath * @param newImg * @param 
     * per 
     */  
    private static void OutImage(String outImgPath, BufferedImage newImg) {  
        // �ж�������ļ���·���Ƿ���ڣ��������򴴽�  
    	System.out.println(outImgPath);
        File file = new File(outImgPath);  
//        if (!file.exists()) 
//        {  
//            file.mkdirs();  
//        }// ������ļ���  
        
         if (!file.getParentFile().exists()) {  
            file.getParentFile().mkdir();  
         }// ������ļ���
        
       
        
        System.out.println("outImgPath" +outImgPath.substring(outImgPath.lastIndexOf(".") + 1) );
        try {  
            ImageIO.write(newImg, "png", new File(outImgPath));  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
    
    
 

    
    /**
     * ���һ��ָ����С��·����͸������ͼƬ
     * @param transparentWidth
     * @param transparentHeight
     * @param toPath
     * @throws IOException
     */
	private synchronized static void drawTransparent(int transparentWidth,
			int transparentHeight, String toPath) throws IOException {
		int width = transparentWidth;
		int height = transparentHeight;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = image.createGraphics();
		image = g2d.getDeviceConfiguration().createCompatibleImage(width,
				height, Transparency.TRANSLUCENT);
		g2d.dispose();
		g2d = image.createGraphics();
		g2d.setStroke(new BasicStroke(1));
		// �ͷŶ���
		g2d.dispose();
		// �����ļ�

		ImageIO.write(image, "png", new File(toPath));
		
	}

 
	 
	/**
	 * �������ͼ-ָ�������߿�����ֵ��ѹ��ͼƬ 
	 * @param transparentPath:ָ���Ļ�����С����һ��ָ����С��͸������ͼƬ
	 * @param srcImgPath:ԴͼƬ·�� 
	 * @param outImgPath  :�����ѹ��ͼƬ��·�� 
	 * @param maxLength :�����߿�����ֵ 
	 * @throws IOException
	 */
    public synchronized static void createThumbnailImg(int transparentWidth,int transparentheight,String transparentPath,String srcImgPath, String outImgPath,  
			int maxLength) throws IOException {

    	String transPath=transparentPath +File.separator+transparentWidth+"_"+transparentheight+".png";
     
    	if(!(new File(transPath).exists()))
		{
    	drawTransparent(transparentWidth,transparentheight,transPath);
		}
    	
    	
		BufferedImage src = null;
		InputStream is = null;
		InputStream is2 = null;
		//OutputStream os = null;

		 BufferedImage transparentImage = null;
		 
		try {
			//�����ͼ
			is = new FileInputStream(transPath);
			//����ԴͼƬ
			is2 = new FileInputStream(srcImgPath);
			
			//����
			transparentImage=javax.imageio.ImageIO.read(is);
			src = javax.imageio.ImageIO.read(is2);
			
			//ԴͼƬ���ղŴ���
			if (null != src)
			{
				// �õ�Դͼ��
				int old_w = src.getWidth();
				
				// �õ�Դͼ��
				int old_h = src.getHeight();
				
				// ��ͼ�Ŀ�
				int new_w = 0;
				
				// ��ͼ�ĳ�
				int new_h = 0;
				
				// ���ͼƬ�ߴ�ѹ���ȵõ���ͼ�ĳߴ�
				//����ǿ�ȸߴ����Կ�ΪĿ������������ 
				//����ֵ�̶����߶����ŵı���=maxLength/Դͼ��
				//maxLength����Ϊ�Ŵ�maxLengthС��Ϊ��С
				if (old_w > old_h) {
					// ͼƬҪ���ŵı���
					new_w = maxLength;
					new_h = (int)  Math.round(old_h
							* ((float) maxLength / old_w));
				} else {
					new_w = (int)  Math.round(old_w
							* ((float) maxLength / old_h));
					new_h = maxLength;
				}

				// �����ͼ
				BufferedImage newImg = null;
				// �ж�����ͼƬ������
				//System.out.print("src.getType():" + src.getType());
				switch (src.getType()) {
				case 13:
					// png,gif
					newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_4BYTE_ABGR);
					break;
				default:
					newImg = new BufferedImage(new_w, new_h,
							BufferedImage.TYPE_INT_RGB);
					break;
				}
				Graphics2D g2d = newImg.createGraphics();

				g2d.getDeviceConfiguration().createCompatibleImage(new_w,
						new_h, Transparency.TRANSLUCENT);
				// ��ԭͼ��ȡ��ɫ������ͼ
				g2d.drawImage(src, 0, 0, old_w, old_h, null);
				g2d.dispose();
				// ���ͼƬ�ߴ�ѹ���ȵõ���ͼ�ĳߴ�
				newImg.getGraphics()
						.drawImage(
								src.getScaledInstance(new_w, new_h,
										Image.SCALE_SMOOTH), 0, 0, null);

				//========================���˴���ͼ�������=============================
				//========================���ڿ�ʼ����ͼ����ͼ�ĺϲ�=====================
				// ��ͼ
				BufferedImage image = transparentImage;
				// Ҫ��ӵ�ͼƬ
				BufferedImage image2 = newImg;
				// �õ���ͼ������
				Graphics g = image.getGraphics();
				// ��ʼ����
				// �������Ҫ��ӵ�ͼ��������ӵ�λ����
				// ��ͼ��ȡ2��֮1��ȥҪ����ͼ��ȡ2��֮1��ΪX���
				// ��ͼ��ȡ2��֮1��ȥҪ����ͼ��ȡ2��֮1��ΪY���
				// ---------------
				// | __________  |
				// | |         | |
				// | |         | |
				// | |_________| |
				// |             |
				// ---------------
				// �������������Ͻ�(image.getWidth()/2-image2.getWidth()/2,image.getHeight()/2-image2.getHeight()/2)��ԭʼ��С��ʾһ��ͼ��
				g.drawImage(image2, image.getWidth() / 2 - image2.getWidth()/ 2, 
						            image.getHeight() / 2 - image2.getHeight() / 2,
						    null);

				// ���

				File file = new File(outImgPath);

				if (!file.exists()) {
					file.mkdirs();
				}
				//if (!file.getParentFile().exists()) {
				//	file.getParentFile().mkdir();
				//}
				// ������ļ���

			 

				ImageIO.write(image, "png", new File(outImgPath));

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				is.close();
			}

			if (is2 != null) {
				is2.close();
			}
		}
	}
    	 
  
	
    public static void main(String[] args) {
		// TODO Auto-generated method stub
        ImageUtil iu = new ImageUtil();
    	 
    	 iu.compressImage("D:\\temp\\1423754219501627123626.jpg", "d:\\aaa\\dd.jpg", 160);
      	
    	 iu.compressImage("D:\\temp\\1423754219501358235328.jpg", "d:\\aaa\\ff.jpg", 160);
    	
    	 iu.compressImage("d:\\aaa\\ff.jpg", "d:\\aaa\\ee.jpg", 160, 160);
    	
    	
    	
     
      
		
 		try {
 			
 			int transparentWidth=200;
 			int transparentheight=160;
 			int maxLength=100;
 			String transparentPath="D:\\aaa";
 			String srcPath1="D:\\temp\\1423754219501627123626.jpg";
 			String srcPath2="D:\\temp\\1423754219501358235328.jpg";
 			String outputPath1="D:\\aaa\\a2.jpg";
 			String outputPath2="D:\\aaa\\b2.jpg";
 			//ָ��·���ĵ�ͼ�����ھͲ����������
 		 
 				 
 				  
 			if(transparentWidth>transparentheight)
 			{
 				maxLength=transparentWidth;
 			}
 			else
 			{
 				maxLength=transparentheight;
 			}
 			
 			//ָ��·���ĵ�ͼ�����ھͲ����������
 		 
 				 
 			createThumbnailImg(transparentWidth, transparentheight,transparentPath, srcPath1,   outputPath1, maxLength) ;
 			createThumbnailImg(transparentWidth, transparentheight,transparentPath, srcPath2,   outputPath2,   maxLength) ;
 			 
 	 
 			
 		
 			 
 		
 		} catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
		  
		 
		   
	}
    
    
}  
